package collection.Demo11;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * @Author Miku
 * @Create 2021/4/17 16:57
 * 使用TreeSet集合实现字符串按照长度进行排序
 * Comparator接口实现定制比较
 */
public class Test {
    public static void main(String[] args) {
        TreeSet<String> treeSet = new TreeSet<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                int n1 = o1.length() - o2.length();
                int n2 = o1.compareTo(o2);

                return n1 == 0 ? n2:n1;
            }
        });

        treeSet.add("helloworld");
        treeSet.add("pingguo");
        treeSet.add("anzhuo");
        treeSet.add("xian");
        treeSet.add("miku");
        treeSet.add("test");
        treeSet.add("xiaomi");

        System.out.println(treeSet);

    }
}
